package cn.shop.admin.service.impl;

import cn.shop.admin.req.AppRewardReq;
import cn.shop.admin.service.PrizeRuleService;
import cn.shop.admin.service.PromotionsService;
import cn.shop.base.MyServiceImpl;
import cn.shop.transcation.promotionsModule.Promotions;
import cn.shop.transcation.promotionsModule.mapper.PromotionsMapper;
import cn.shop.utils.exception.ServiceException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 活动表(Promotions)表服务实现类
 *
 * @author 痴
 * @since 2022-06-25 10:46:27
 */
@Service("promotionsService")
public class PromotionsServiceImpl extends MyServiceImpl<PromotionsMapper, Promotions> implements PromotionsService {

    @Resource
    private PrizeRuleService prizeRuleService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveAppActivity(AppRewardReq req) {
        Promotions promotions = lambdaQuery()
                .eq(Promotions::getKey, "APP_REWARD")
                .one();
        promotions.setContent(req.getContent());
        promotions.setPrizePool(req.getPrizePool());
        boolean success = updateById(promotions);
        if (success) {
            req.getPrizeRules().forEach(r -> r.setPid(promotions.getId()));
            success = prizeRuleService.saveOrUpdateBatch(req.getPrizeRules());
            if (!success) {
                throw new ServiceException("保存活动规则出错");
            }
        }
        return success;
    }
}

